package com.truckdispatching.http.controller;

import java.util.HashMap;

import org.json.JSONObject;

import com.truckdispatching.UserManager;
import com.truckdispatching.http.util.TDHttpController;

/**
 * 版本更新请求
 * 
 * @author Lee
 * @date 2013-5-9
 */
public class CheckUpdateHttpController extends TDHttpController {

	@Override
	public String getRequestUrl() {
		return mHostUrl + "/wap/checkUpdate.do";
	}

	public void sendRequest(String version) {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("version", version);
		params.put("jsessionid", UserManager.getToken());
		sendRequest(params, false);
	}

	@Override
	public void onSuccess(String response) {
		try {
			JSONObject jo = getJSONObjectFromJSON(new JSONObject(response), "data", null);
			String version = getStringFromJSON(jo, "version", "");
			if (version != null && !version.equals("")) {
				String url = getStringFromJSON(jo, "url", "");
				boolean force = getBooleanFromJSON(jo, "force", false);
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("version", version);
				map.put("url", url);
				map.put("force", force);
				callOnResponseListener(map);
			} else {
				callOnResponseListener(null);
			}
		} catch (Exception e) {
			callOnResponseErrorListener(-1, "JSON数据异常");
		}

	}

	@Override
	public void onError(int errorCode, String errorMessage) {
		callOnResponseErrorListener(errorCode, errorMessage);
	}
}
